<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-tabs type="border-card">
			<el-tab-pane ref="tab" label="基础信息">
				<el-form
					:model="form"
					:rules="rules"
					ref="dialogForm"
					label-width="100px"
					label-position="right"
				>
					<el-row>
						<el-col :span="8">
							<el-form-item label="编号" prop="code">
								<el-input
									label="编号"
									placeholder="自动生成"
									v-model="form.code"
									readonly
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="入库日期" prop="in_date">
								<vxe-input
									v-model="form.in_date"
									placeholder="日期选择"
									type="date"
								></vxe-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="供应商" prop="company">
								<skAutoChoose
									v-model="form.company.name"
									:name="'company'"
									:assign="form"
								>
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="11">
							<el-form-item label="备注" prop="remark">
								<el-input
									v-model="form.remark"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<sc-VxeTable
						ref="xGrid1"
						:gridEvts="gridEvents"
						:data="form.details"
						:gridOps="gridOps"
						:tablecode="'storageActionPurchaseSave'"
					>
					</sc-VxeTable>
				</el-form>
			</el-tab-pane>
		</el-tabs>

		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>

	<skBrowserChoose
		v-model="openChoose.order"
		:name="'purchase_order_storage'"
		@close="openChoose.order = false"
		@select="orderSelectChoose"
	></skBrowserChoose>
</template>

<script>
import skBrowserChoose from "@/components/skBrowserChoose";
import skAutoChoose from "@/components/skAutoChoose";
import XEUtils from "xe-utils";
export default {
	emits: ["success", "closed"],
	components: { skBrowserChoose, skAutoChoose },
	data() {
		return {
			openChoose: { order: false },
			mode: "edit",
			gridEvents: {
				fromArrival: () => {
					this.openChoose.order = true;
				},
			},
			gridOps: {
				height: 300,
				columns: [
					{
						field: "material.name",
						event: {
							select: ({ e, row }) => {
								row["material"]["spec"] = e.spec;
							},
						},
					},
					{
						field: "auxiliary_quantity",
						event: {
							keyup: ({ row }) => {
								row["quantity"] =
									row["auxiliary_quantity"] * 25;
								row["fect_quantity"] = row["quantity"];

								row["fect_auxiliary_quantity"] =
									row["auxiliary_quantity"];
							},
						},
					},
					{
						field: "quantity",
						event: {
							keyup: ({ row }) => {
								row["fect_quantity"] = row["quantity"];
							},
						},
					},
					{
						field: "fect_auxiliary_quantity",
						event: {
							keyup: ({ row }) => {
								row["fect_quantity"] =
									row["fect_auxiliary_quantity"] * 25;
							},
						},
					},
				],
			},
			titleMap: {
				add: "新增",
				edit: "编辑",
			},

			form: {
				id: "",
				code: "",
				company: { name: "" },
				details: [],
			},
			rules: {
				// billdate: [{ required: true, message: "请输入型号规格" }],
			},
			visible: false,
			isSaveing: false,
		};
	},
	mounted() {},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;

			return this;
		},
		orderSelectChoose(row) {
			this.openChoose.order = false;

			row.filter((v) => {
				if (
					typeof this.form.company_id == "undefined" ||
					this.form.company_id == 0
				) {
					Object.assign(this.form.company, v.company);
					this.form.company_id = v.company.id;
					v["purchase_order_id"] = v["id"];
					v["id"] = "key" + v["purchase_order_id"];

					//剩余数量
					v.auxiliary_quantity =
						v.remain_quantity / (v.quantity / v.auxiliary_quantity);
					v.quantity = v.remain_quantity;
					v["remain_quantity"] = v["quantity"];
					this.form.details.push(v);
					return v;
				} else if (this.form.company_id != v.company.id) {
					this.$alert("供应商不同！请重新选择", "提示", {
						type: "error",
					});
					return false;
				}
			});
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;

					var res = await this.$API.purchase.storage.save.post(
						this.form
					);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					}
				}
			});
		},
		async getItems(id) {
			var res = await this.$API.purchase.storage.list.get({
				filter: [{ main_id: id }],
			});
			if (res.code == 200) {
				this.form.details = res.data.rows;
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data.main);
			if (this.form.in_date == null) {
				this.form.in_date = XEUtils.toDateString(
					new Date(),
					"yyyy-MM-dd"
				);
			}

			this.getItems(data.main.id);
		},
	},
};
</script>

<style></style>
